package com.abc.recycling.config;

import com.abc.recycling.model.User;
import com.abc.recycling.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

@Slf4j
@Component
@RequiredArgsConstructor
public class DataInitializer implements CommandLineRunner {

    private final UserService userService;

    @Override
    public void run(String... args) {
        try {
            // 检查admin用户
            User existingAdmin = userService.findByUsername("admin");
            if (existingAdmin == null) {
                log.info("创建默认管理员账户");
                // 创建新的admin用户
                User admin = new User();
                admin.setUsername("admin");
                admin.setPassword("123456"); // 使用明文密码
                admin.setRole("ADMIN");
                admin.setStatus(1);
                admin.setEmail("admin@recycling.com");
                admin.setPhone("13800000000");
                admin.setRealName("系统管理员");
                userService.register(admin);
                log.info("默认管理员账户创建成功");
            }
        } catch (Exception e) {
            log.error("初始化数据时发生错误: {}", e.getMessage());
        }
    }
} 